package com.zrrd.javaSEPlus._02_enum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author WanJl
 * @version 1.0
 * @title CompareDemo
 * @description
 * @create 2023/11/11 18:52
 */
public class CompareDemo {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "张三", 25));
        list.add(new Student(2, "李四", 15));
        list.add(new Student(3, "王五", 55));
        list.add(new Student(4, "赵六", 46));
        list.add(new Student(5, "李磊", 72));
        list.add(new Student(6, "李雷", 16));
        System.out.println(list);

        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getAge() == o2.getAge())
                    return 0;
                else if (o1.getAge() > o2.getAge())
                    return 1;
                else
                    return -1;
            }
        });
        System.out.println(list);

    //-------------------------------------------------------------------------

        Student s1 = new Student(6, "李雷", 16);
        Student s2 = new Student(5, "李磊", 72);

        System.out.println(s1.compareTo(s2));


    }
}

class Student implements Comparable<Student> {
    private int id;
    private String name;
    private int age;

    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id && age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }

    @Override
    public int compareTo(Student s) {
        if (this.age == s.age)
            return 0;
        else if (this.age > s.age)
            return 1;
        else
            return -1;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
